/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.Parametro;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Administrador
 */
public class AdminParametro implements ParametroManager{
    private Connection c;
    
    public AdminParametro(Connection c){
        this.c = c;
    }
    @Override
    public Parametro getParametro() throws DataException {
        Parametro p = new Parametro();
        ResultSet rs = null;
        PreparedStatement preSt = null;
        try{
            String query = "SELECT IDPARAMETRO";
                   query += ", VENTA_MATRICULA_POR_FACULTAD";
                   query += " FROM PARAMETRO LIMIT 1";            
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            while(rs.next()){
                p.setIdParametro(rs.getInt("IDPARAMETRO"));
                p.setVentaMatriculaXfacultad(rs.getBoolean("VENTA_MATRICULA_POR_FACULTAD"));
            }
        }catch(SQLException e){
            throw new DataException(" AdminParametro -> getParametro() e = "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        return p;
    }
    @Override
    public void updateParametro(Parametro p) throws DataException {
        PreparedStatement preSt = null;
        try{
            String query = "UPDATE PARAMETRO SET VENTA_MATRICULA_POR_FACULTAD = "+p.isVentaMatriculaXfacultad();
            query+= " WHERE IDPARAMETRO="+p.getIdParametro();
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException ex){
            throw new DataException(" AdminParametro -> updateParametro(Parametro p) e = "+ex.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
}
